package com.lst.servlet;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.AnswerReqPara;
import com.lst.entity.response.ClassPictureResPara;
import com.lst.model.ClassPicture;
import com.lst.service.ClassPictureService;
import com.lst.service.OnlineCourseService;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: ClassPictureListServlet
 * @Description: 功夫分类列表接口
 * 
 */
public class ClassPictureListServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private ClassPictureService classPictureService = ctx.getBean(ClassPictureService.class);
	
	private OnlineCourseService onlineCourseService = ctx.getBean(OnlineCourseService.class);
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		ClassPictureResPara resPara = new ClassPictureResPara();

		try {
			BaseRequest reqPara = getReqPara(request);
		
		if(isCheckField(reqPara,resPara)){
			Integer page = Integer.valueOf(reqPara.getPage());
			Integer pagesize = Integer.valueOf(reqPara.getPagesize());	
			
			//根据时间查询列表
			Map<String, Object> reqMap = new HashMap<String, Object>();		
			reqMap.put("orderByClause", "md.id");

			PageBounds pageBounds = new PageBounds(page,pagesize);
			List<ClassPicture> classPictureList=classPictureService.queryByList(reqMap, pageBounds);
			if(classPictureList!=null&&classPictureList.size()>0){
				for(ClassPicture classPicture:classPictureList){
					if(classPicture.getId()==1){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class1);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
					if(classPicture.getId()==2){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class2);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
					if(classPicture.getId()==3){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class3);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
					if(classPicture.getId()==4){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class8);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
					if(classPicture.getId()==5){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class9);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
					if(classPicture.getId()==6){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class10);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
					if(classPicture.getId()==7){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class11);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
					if(classPicture.getId()==8){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class12);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
					if(classPicture.getId()==9){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class13);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
					if(classPicture.getId()==10){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class14);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
					if(classPicture.getId()==11){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class15);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
					if(classPicture.getId()==12){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class16);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
//					if(classPicture.getId()==13){
//						Map<String, Object> map = new HashMap<String, Object>();		
//						map.put("type", CommCode.Course_Class4);
//						int num=onlineCourseService.countByMap(map);
//						classPicture.setTotalNum(num);
//					}
					if(classPicture.getId()==14){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class5);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
					if(classPicture.getId()==15){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class6);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
					if(classPicture.getId()==16){
						Map<String, Object> map = new HashMap<String, Object>();		
						map.put("type", CommCode.Course_Class7);
						int num=onlineCourseService.countByMap(map);
						classPicture.setTotalNum(num);
					}
					
				}
			}
			
		    resPara.setClassPictureList(classPictureList);
			resPara.setCode(CommCode.M_Y000000);
			resPara.setMessage(CommCode.M_Y000001);
		  }
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}
		
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
		//	logger.info("ClassPictureListServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

		//	logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public AnswerReqPara getReqPara(HttpServletRequest request) {

		AnswerReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new AnswerReqPara());

			reqPara = (AnswerReqPara) baseRequest.clone();

			String page = request.getParameter("page");
			String pagesize = request.getParameter("pagesize");
			
//			logger.info("reqPara page: " + page);
//			logger.info("reqPara pagesize: " + pagesize);
		
			reqPara.setPage(page);
			reqPara.setPagesize(pagesize);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		BaseRequest cdReqPara = (BaseRequest) reqPara;
		BaseResponse cdResPara = (BaseResponse) resPara;

      if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);
		}else if (StringUtils.isEmpty(cdReqPara.getPage())){
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000009);	
		} else if (StringUtils.isEmpty(cdReqPara.getPagesize())){
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000010);				
		} else {
			flag = true;
		}

		return  flag;
	}

}
